<template>
	<view >
		<view class="box">
			<view class="input_box">
				<view class="label">联系人</view>
				<view class="input">
					<input type="text" v-model="info.consignee" placeholder="请填写联系人姓名" />
				</view>
			</view>
			<view class="input_box">
				<view class="label">手机号</view>
				<view class="input">
					<input type="text" v-model="info.mobile" placeholder="请填写手机号" />
				</view>
			</view>
			<view class="input_box">
				<view class="label">所在地区</view>
				<AddressPicker style="width: 80%;" @change="change" :level="3">{{planLocate}} </AddressPicker>

			</view>
			<view class="input_box input_box2">
				<view class="label">详细地址</view>
				<view class="input">
					<textarea class="textarea2" v-model="info.address" placeholder="街道丶楼牌号" />
				</view>
			</view>
		</view>
		<view
			style="display: flex;width: 25%;color: white; height: 60rpx;border-style: outset; background-color: blue;justify-content: center;align-items: center;box-shadow: 1px 1px 5px #ffffff;margin: 20rpx auto 0 auto;"
			@click="addresssave">
			兑 换
		</view>
	</view>
</template>

<script>
	import AddressPicker from "@/components/AddressPicker/AddressPicker.vue"
	export default {
		components: {
			AddressPicker
		},
		data() {
			return {
				planLocate: '请选择地址',
				info: {
					"address": "",
					"city": "",
					"cityId": 0,
					"consignee": "",
					"district": "",
					"districtId": 0,
					"isDefault": false,
					"mobile": "",
					"province": "",
					"provinceId": 0,
				},
				address: '',
			}
		},
		// 点修改地址传回的是带ID参数的网址
		async onLoad(options) {
			this.kahao = options.kahao
			this.mima = options.mima
			// 时间戳转格式化字符串（本地时间）
			this.formatDate = (timestamp) => {
				const date = new Date(timestamp);
				const year = date.getFullYear();
				const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份+1补零
				const day = String(date.getDate()).padStart(2, '0');
				const hours = String(date.getHours()).padStart(2, '0');
				const minutes = String(date.getMinutes()).padStart(2, '0');
				const seconds = String(date.getSeconds()).padStart(2, '0');
				return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
			};
		},
		methods: {
			//更改三级地址 =确认时运行 有code和name 
			change(result) {
				// 将选择的结果给值
				this.info.provinceId = result.data[0].id
				this.info.province = result.data[0].name
				this.info.cityId = result.data[1].id
				this.info.city = result.data[1].name
				this.info.districtId = result.data[2].id
				this.info.district = result.data[2].name
				console.log("info", this.info)
				// 将最后的省市区给到planlocate,显示到前端
				this.planLocate = '';
				result.data.forEach((item, index) => {
					if (index === 0) {
						this.planLocate += item.name
					} else {
						this.planLocate += '-' + item.name
					}
				});
				let arr = this.planLocate.split('-');
				this.address = arr[0] + "-" + arr[1] + "-" + arr[2];

			},
			addresssave() { //点保存运行
				// 检查手机号是否正确
				let myreg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
				if (!myreg.test(this.info.mobile)) {
					return uni.showToast({
						title: '手机号格式不正确',
						icon: 'none',
						mask: true
					})
				}
				// 检查是否为空
				if (this.info.consignee == '' || this.info.address == '' || this.planLocate == '请选择地址') {
					return uni.showToast({
						title: '请填写完整信息',
						icon: 'none',
						mask: true
					})
				}
				// 检查是否为空
				uni.showLoading({
					title: '兑换中……',
					mask: true
				})
				// 开始兑换操作 一写入lipin数据，二写入duihuanlist表
				this.info.kahao = this.kahao
				this.info.mima = this.mima
				this.info.status=0
				this.info.creat_time = this.formatDate(new Date().getTime());
				// 获取当前时间对象
				const currentDate = new Date();
				// this.kahao是以E1开头则执行
				if (this.kahao.substring(0, 2) == "E1") {
					currentDate.setDate(currentDate.getDate() + 30);
				} else if (this.kahao.substring(0, 2) == "10") {
					currentDate.setDate(currentDate.getDate() + 15);
				}
				this.next_duihuantime = this.formatDate(currentDate);
				uniCloud.callFunction({
					name: "fun1",
					data: {
						leixing: 2, //兑换
						shuju: this.info,
						next_duihuantime: this.next_duihuantime
					}
				}).then(res => {
					this.info = {}
					uni.hideLoading()
					uni.showModal({
						title: '操作确认',
						content: '兑换成功，请耐心等待快递送货上门',
						confirmText: '确定',
						success: (res) => {
							if (res.confirm) {
								// 跳转到网页pages/address/address
								uni.navigateTo({
									url: '/pages/shouye/shouye'
								})
							}
						},
						showCancel: false
					})
				})
			},
		}
	}
</script>

<style scoped lang="scss">
	.box {
		margin: 20rpx 0 0;
		background: #fff;
		position: relative;
		.input_box2 {
			align-items: flex-start !important;
		}
		.input_box {
			font-size: 28rpx;
			display: flex;
			align-items: center;
			border-bottom: 1rpx solid #F6F6F6;
			padding: 30rpx 30rpx;
			.label {
				width: 160rpx;
				text-align: left;
			}
			.input {
				font-size: 28rpx;
				width: calc(100% - 160rpx);
				position: relative;
				.textarea2 {
					font-size: 28rpx;
					height: 100rpx;
					width: 100%;
				}
			}
		}
	}
	input {
		font-size: 28rpx;
	}
</style>